package ru.tulupov.collectionsbenchmark.tester;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;

public abstract class ListTester<T extends Comparable<T>> extends Tester<T> {
	private static final Logger LOGGER = Logger.getLogger(ListTester.class);
	private List<T> list;

	public ListTester(List<T> list) {
		super(list);
		this.list = list;
	}

	public void sort() {
		Collections.sort(list);
		LOGGER.info("Sorting list.");
	}

	@Override
	public void randomAccess(int count) {
		SecureRandom random = new SecureRandom();
		int hash = 0;
		for (int i = 0; i < count; i++) {
			T obj = list.get(random.nextInt(list.size()));
			hash += obj.hashCode();
		}
		LOGGER.info(String.format("Random access list [%d].", hash));
	}

	public void randomInsertion(int count) {
		SecureRandom random = new SecureRandom();
		for (int i = 0; i < count; i++) {
			list.add(random.nextInt(list.size()), null);
		}
		LOGGER.info("Random insertion.");
	}

	public void randomDeletion(int count) {
		SecureRandom random = new SecureRandom();
		for (int i = 0; i < count; i++) {
			list.remove(random.nextInt(list.size()));
		}
		LOGGER.info("Random deletion.");
	}

}
